package com.imooc.service;

import com.imooc.mapper.UsersFansMapper;
import com.imooc.mapper.UsersMapper;
import com.imooc.pojo.Users;
import com.imooc.pojo.UsersFans;
import com.imooc.utils.MD5Utils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
@SuppressWarnings("all")
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UsersFansMapper usersFansMapper;
    @Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUserNameIsExist(String username) {
        /*
         * 根据条件查询用户
         */
        Users user = new Users();
        user.setUsername(username);

        Users result = usersMapper.selectOne(user);
        return result == null ? false : true;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users saveUser(Users user) {
        //实现全局唯一的id
        String userId = sid.nextShort();

        Users users = new Users();
        users.setId(userId);
        users.setUsername(user.getUsername());
        try {
            users.setPassword(MD5Utils.getMD5Str(user.getPassword()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        users.setNickname(user.getUsername());
        users.setFansCounts(0);
        users.setFollowCounts(0);
        users.setReceiveLikeCounts(0);
        usersMapper.insert(users);
        return users;

    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Object login(Users users) {
        boolean b = queryUserNameIsExist(users.getUsername());
        if (b) {
            try {
                String password = users.getPassword();
                String md5Str = MD5Utils.getMD5Str(password);
                users.setPassword(md5Str);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            //    Users result = usersMapper.selectOne(users);
            //这里可以根据条件查询 criteria 里面包含许多方法 比如 like Between
            Example example = new Example(Users.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("username", users.getUsername());
            criteria.andEqualTo("password", users.getPassword());
            Users result = usersMapper.selectOneByExample(example);

            if (result == null) {
                //密码错误
                return "密码错误";
            } else {
                result.setPassword("");
                return result;

            }

        } else {
            //用户不存在
            return "用户不存在";
        }
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUserInfo(Users users) {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", users.getId());

        usersMapper.updateByExampleSelective(users, example);
    }

    /**
     * 查询用户信息
     *
     * @param userId
     */
    @Override
    public Users queryUserInfo(String userId) {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", userId);
        Users users = usersMapper.selectOneByExample(example);
        return users;
    }

    @Override
    public Boolean isFans(String userId, String fanId) {
        Example example = new Example(UsersFans.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("fanId", fanId);
        List<UsersFans> usersFans = usersFansMapper.selectByExample(example);
        if (usersFans != null && usersFans.size() > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Boolean addFansCounts(String userId, String fanId) {

        UsersFans usersFans = new UsersFans();
        usersFans.setUserId(userId);
        usersFans.setFanId(fanId);
        usersFans.setId(sid.nextShort().toString());

        usersFansMapper.insert(usersFans);

        Boolean aBoolean = usersMapper.addFansCounts(userId);
        return aBoolean;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Boolean reduceFansCounts(String userId, String fanId) {

        Example example = new Example(UsersFans.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("fanId", fanId);
        int i = usersFansMapper.deleteByExample(example);
        System.out.println("删除的:" + i);
        if (i == 0) {
            return false;
        }
        Boolean aBoolean = usersMapper.reduceFansCounts(userId);
        return aBoolean;
    }


}
